var Config = require("../config");
var KOC = require("koc.common.utils");
var QueryString = require("querystring");
var Iconv = require('iconv-lite');
var Interface_Heepay = module.exports = {
    _NotifyURI: Config.Common.Virtual_Path.Main + "heepay/business.user-payoney",
    _ReturnURI: Config.Common.Virtual_Path.Main + "member/user",
    Send: function (_parm) {
        var Web = {
            Version: Config.Interface.Heepay.WEB.Version,
            AgentID: Config.Interface.Heepay.WEB.AgentID,
            Key: Config.Interface.Heepay.WEB.Key,
            DateTime: moment(new Date()).format("YYYYMMDDhhmmss"),
            Meta_option: '{"s":"WAP","n":"XiuTV","id":"' + Config.Common.Virtual_Path.Main + '"}',
            Sign: '',
            Weixin: '',//微信支付
            HeepaySend: ''
        };
        Web.Meta_option = new Buffer(Iconv.encode(Web.Meta_option, 'GB2312')).toString('base64');
        if (_parm.is_phone && _parm.is_frame) {
            Web.Weixin = "&is_phone=" + _parm.is_phone + "&is_frame=" + _parm.is_frame + "&meta_option=" + Web.Meta_option;
            Interface_Heepay._ReturnURI=Config.Common.Virtual_Path.Main +"mobile/pay"
        }
        Web.Sign = "version=" + Web.Version +
            "&agent_id=" + Web.AgentID +
            "&agent_bill_id=" + _parm.ordercode +
            "&agent_bill_time=" + Web.DateTime +
            "&pay_type=" + _parm.pay_type +
            "&pay_amt=" + _parm.pay_amt +
            "&notify_url=" + Interface_Heepay._NotifyURI +
            "&return_url=" + Interface_Heepay._ReturnURI +
            "&user_ip=" + _parm.IP.replace(/\./g, "_") +
            "&key=" + Web.Key;
        Web.HeepaySend = "https://pay.Heepay.com/Payment/Index.aspx?" +
            "version=" + Web.Version +
            "&agent_id=" + Web.AgentID +
            "&agent_bill_id=" + _parm.ordercode +
            "&pay_type=" + _parm.pay_type +
            "&pay_code=" + "" +
            "&pay_amt=" + _parm.pay_amt +
            "&notify_url=" + Interface_Heepay._NotifyURI +
            "&return_url=" + Interface_Heepay._ReturnURI +
            "&user_ip=" + _parm.IP.replace(/\./g, "_") +
            "&agent_bill_time=" + Web.DateTime +
            "&goods_name=QuanMinXiuTV" +
            "&goods_num=1" +
            "&remark=QuanMinXiuTV" +
            "&sign=" + KOC.StringUtils.MD5(Web.Sign);
        if (Web.Weixin) {
            Web.HeepaySend = Web.HeepaySend + Web.Weixin;
        }
        return Web.HeepaySend;

    }
};